# Contributor: Mathias LANG <pro.mathias.lang@gmail.com>
# Maintainer: Mathias LANG <pro.mathias.lang@gmail.com>
pkgname=dmd
pkgver=2.104.2
pkgrel=1
pkgdesc="D Programming Language reference compiler"
url="https://github.com/dlang/dmd"
# TODO: Enable on x86 once LDC-1.20.1 is out (and enabled on x86)
arch="x86_64"
license="BSL-1.0"
depends="llvm-libunwind-dev tzdata"
makedepends="chrpath ldc"
subpackages="$pkgname-doc"
source="dmd-$pkgver.tar.gz::https://github.com/dlang/dmd/archive/v$pkgver.tar.gz
	phobos-$pkgver.tar.gz::https://github.com/dlang/phobos/archive/v$pkgver.tar.gz

	dmd-install-config.conf
	lfs64.patch
	10-dmd-musl.patch
	"
builddir="$srcdir"
options="!check" # todo

prepare() {
	# The Makefiles make some assumption about the directory structure
	ln -s "$srcdir/dmd-$pkgver/" "$srcdir/dmd"
	ln -s "$srcdir/phobos-$pkgver/" "$srcdir/phobos"

	default_prepare
}

build() {
	export HOST_DMD="ldmd2"

	local generated="$builddir"/dmd/generated

	cd "$builddir"/dmd
	mkdir generated
	ldmd2 -ofgenerated/build -g compiler/src/build.d -release -O
	generated/build BUILD=release HOST_DMD="$HOST_DMD" CXX="c++" ENABLE_RELEASE=1 DFLAGS="$DFLAGS" dmd -v $MAKEFLAGS

	cd "$builddir"/dmd/druntime
	make -f posix.mak DMD="$generated"/linux/release/*/dmd BUILD=release ENABLE_RELEASE=1 PIC=1

	cd "$builddir"/phobos
	make -f posix.mak DMD="$generated"/linux/release/*/dmd BUILD=release ENABLE_RELEASE=1 PIC=1

	cd "$builddir"/dmd/compiler
	make -C docs DMD="$HOST_DMD"

	# Strip redundant rpath to avoid warnings in the builder
	chrpath -d "$builddir"/dmd/generated/linux/release/64/dmd
}

package() {
	mkdir -p "$pkgdir"/usr/lib/ "$pkgdir"/usr/include/dmd/phobos/

	install -Dm755 dmd/generated/linux/release/64/dmd -t "$pkgdir"/usr/bin/
	install -Dm644 dmd-install-config.conf "$pkgdir"/etc/dmd.conf

	install -Dm644 dmd/generated/docs/man/man1/dmd.1 -t "$pkgdir"/usr/share/man/man1/
	install -Dm644 dmd/generated/docs/man/man5/* -t "$pkgdir"/usr/share/man/man5/

	install -Dm755 phobos/generated/linux/release/64/*.so* -t "$pkgdir"/usr/lib/
	install -Dm755 phobos/generated/linux/release/64/*.a -t "$pkgdir"/usr/lib/

	cp -r phobos/etc phobos/std phobos/*.d "$pkgdir"/usr/include/dmd/phobos/
	cp -r dmd/druntime/import "$pkgdir"/usr/include/dmd/druntime
}

sha512sums="
1da67bdce9cf27e99e39803fb78e48c86f74b1b881d8a903e37e615331cc988a01ab2026a03350858f98335f518ba2ee0eaa337488cf8c380ef66635f367fa09  dmd-2.104.2.tar.gz
e04691f917267e3d9aa91b2a4a242bd969e572cb598e256a78d4f34e946097d4b97a87d7d2f30a480491d1a2c6eed40c6a138b95d3a4250d9e8267b5811ba789  phobos-2.104.2.tar.gz
123ec0f256a73030a5e5b4b87a7f2e0752320777b7fcd175a221807ec2917f5d6d88776c3448eab077eb7a2211dd4a3d64e3a556053b0f183eb058da437bc5da  dmd-install-config.conf
c8e7de8bc9c43f4ae488cfc35355db2e6cff4278749016631acb2a8c1a2594f8d7ffb9c9a8a6cc2b64e006e9e51d865db3f3a78d05bbdb487f75e415f309d2b8  lfs64.patch
928874c8a6acc593f2ac54b785ff551bc16b53ec647c4c7e19b5f19d609f02b200e550d1ee3d024bf969ef417b705c3448ce590adbe9a113a03e9372718a0f55  10-dmd-musl.patch
"
